// 字符串工具

/**
 * 首字母大写
 * @param str 字符串
 */
export const capitalize = (str: string): string => {
  return str.charAt(0).toUpperCase() + str.slice(1);
};

/**
 * 驼峰转连字符
 * @param str 字符串
 */
export const camelToKebab = (str: string): string => {
  return str.replace(/([A-Z])/g, '-$1').toLowerCase();
};

/**
 * 连字符转驼峰
 * @param str 字符串
 */
export const kebabToCamel = (str: string): string => {
  return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
};

/**
 * 生成UUID
 */
export const generateUUID = (): string => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    const r = (Math.random() * 16) | 0;
    const v = c === 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
};

/**
 * 截取字符串
 * @param str 字符串
 * @param len 长度
 * @param suffix 后缀
 */
export const truncate = (str: string, len: number, suffix = '...'): string => {
  if (str.length <= len) return str;
  return str.slice(0, len) + suffix;
};
